package top.wineye.spring.dependency.source;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.Resource;

@Configuration
@PropertySource(value = "classpath:default.properties", encoding = "utf-8") // 设置 encoding=utf-8 支持中文
public class ExternalConfigByAtValue {

    @Value("${haha.string}")
    String haha;

    // 内建 properties
    @Value("${user.name}")
    String userName;

    @Value("${demo.name.cn}")
    String cn;

    // 自动类型转换
    @Value("${demo.resource}")
    Resource resource;

    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx =
                new AnnotationConfigApplicationContext(ExternalConfigByAtValue.class);


        ExternalConfigByAtValue bean = ctx.getBean(ExternalConfigByAtValue.class);

        System.out.println(bean.haha);
        System.out.println(bean.resource);
        System.out.println(bean.userName);
        System.out.println(bean.cn);

        ctx.close();
    }
}
